<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll">批量删除</el-button>
        <el-input v-model="select_word" size="mini" placeholder="请输入请假人姓名" class="handle-input"></el-input>
        <el-button
          type="primary"
          size="mini"
          @click="centerDialogVisible = true"
          >添加请假单</el-button
        >
      </div>
    </div>
    <!-- 查询 -->
    <el-table
      size="mini"
      ref="multipleTable"
      border
      style="width: 100%"
      height="570px"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column
        prop="name"
        label="请假人姓名"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="startTime"
        label="开始时间"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="endTime"
        label="结束时间"
        width="140"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="reasonLeave"
        label="请假原因"
        width="360"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        background
        layout="total,prev,pager,next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
      >
      </el-pagination>
    </div>

    <!-- 添加 -->
    <el-dialog
      title="添加请假单"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form :model="registerForm" ref="registerForm" label-width="80px">
        <el-form-item prop="name" label="请假人姓名" size="mini">
          <el-input
            v-model="registerForm.name"
            placeholder="请假人姓名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="startTime" label="开始时间" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="registerForm.startTime"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime" label="结束时间" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="registerForm.endTime"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="reasonLeave" label="请假原因" size="mini">
          <el-input
            v-model="registerForm.reasonLeave"
            placeholder="请假原因"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="centerDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" @click="addLeaveManagement">确定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改请假单"
      :visible.sync="editVisible"
      width="400px"
      center
    >
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item prop="name" label="请假人姓名" size="mini">
          <el-input v-model="form.name" placeholder="请假人姓名"></el-input>
        </el-form-item>
        <el-form-item prop="startTime" label="开始时间" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.startTime"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime" label="结束时间" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.endTime"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="reasonLeave" label="请假原因" size="mini">
          <el-input
            v-model="form.reasonLeave"
            placeholder="请假原因"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editVisible = false">取消</el-button>
        <el-button size="mini" @click="editLeaveManagement">确定</el-button>
      </span>
    </el-dialog>

    <!-- 删除 -->
    <el-dialog title="删除请假单" :visible.sync="delVisible" width="300px" center>
      <div align="center">删除不可恢复，是否确定删除？</div>
      <span slot="footer">
        <el-button size="mini" @click="delVisible = false">取消</el-button>
        <el-button size="mini" @click="deleteRow">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { mixin } from "../mixins/index";
import {
  getAllLeaveManagement,
  setLeaveManagement,
  updateLeaveManagement,
  deleteLeaveManagement
} from "../api/index";
export default {
  mixins: [mixin],
  data() {
    return {
      centerDialogVisible: false, //添加弹窗是否显示
      editVisible: false, //编辑弹窗是否显示
      delVisible: false, //删除弹窗是否显示
      tableData: [],
      tempData: [],
      select_word: "",
      pageSize: 5, //分页每页大小
      currentPage: 1, //当前页
      idx: -1, //当前选择项
      multipleSelection: [], //哪些项已经打勾
      // 添加框
      registerForm: {
        name: "",
        startTime: "",
        endTime: "",
        reasonLeave: "",
      },
      // 编辑框
      form: {
        id: "",
        name: "",
        startTime: "",
        endTime: "",
        reasonLeave: "",
      },
    };
  },
  computed: {
    //计算当前搜索结果表里的数据
    data() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  watch: {
    //搜索框里面的内容发生变化的时候，搜索结果table列表的内容跟着它的内容发生变化
    select_word: function () {
      if (this.select_word === "") {
        this.tableData = this.tempData;
      } else {
        this.tableData = [];
        for (let item of this.tempData) {
          if (item.name.includes(this.select_word)) {
            this.tableData.push(item);
          }
        }
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    // 查看所有请假单
    getData() {
      this.tempData = [];
      this.tableData = [];
      getAllLeaveManagement().then((res) => {
        this.tempData = res;
        this.tableData = res;
        this.currentPage = 1;
      });
    },
    // 添加请假单
    addLeaveManagement() {
      let d1 = this.registerForm.startTime;
      let d2 = this.registerForm.endTime;
      let datetime1 =
        d1.getFullYear() + "-" + (d1.getMonth() + 1) + "-" + d1.getDate();
      let datetime2 =
        d2.getFullYear() + "-" + (d2.getMonth() + 1) + "-" + d2.getDate();
      let params = new URLSearchParams();
      params.append("name", this.registerForm.name);
      params.append("startTime", datetime1);
      params.append("endTime", datetime2);
      params.append("reasonLeave", this.registerForm.reasonLeave);

      setLeaveManagement(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("添加成功", "success");
          } else {
            this.notify("添加失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.centerDialogVisible = false;
    },
    // 弹出编辑框
    handleEdit(row) {
      this.editVisible = true;
      this.form = {
        id: row.id,
        name: row.name,
        startTime: row.startTime,
        endTime: row.endTime,
        reasonLeave: row.reasonLeave,
      };
    },
    // 保存编辑页面修改的数据
    editLeaveManagement() {
      let d1 = new Date(this.form.startTime);
      let d2 = new Date(this.form.endTime);
      let datetime1 =
        d1.getFullYear() + "-" + (d1.getMonth() + 1) + "-" + d1.getDate();
      let datetime2 =
        d2.getFullYear() + "-" + (d2.getMonth() + 1) + "-" + d2.getDate();
      let params = new URLSearchParams();
      params.append("id", this.form.id);
      params.append("name", this.form.name);
      params.append("startTime", datetime1);
      params.append("endTime", datetime2);
      params.append("reasonLeave", this.form.reasonLeave);

      updateLeaveManagement(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("修改成功", "success");
          } else {
            this.notify("修改失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.editVisible = false;
    },
    // 删除请假单
    deleteRow(){
      deleteLeaveManagement(this.idx)
      .then(res => {
        if(res) {
          this.getData();
          this.notify("删除成功","success");
        } else {
          this.notify("删除失败","error");
        }
      })
      .catch(err => {
        console.log(err);
      })
      this.delVisible = false;
    }
  },
};
</script>
<style scoped>
.handle-box {
  margin-bottom: 20px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.pagination {
  display: flex;
  justify-content: center;
}
</style>
